package com.lzx.iteam.widget.gridimg;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.util.ImageLoaderInterface;
import io.rong.imageloader.core.ImageLoader;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


public class ImageAdapter extends BaseAdapter {

    private static final String TAG = "ImageAdapter";

    /**
     * 自定义图片的宽高
     */
    protected float imageWidth, imageHeight;

    public float getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(float imageWidth) {
        this.imageWidth = imageWidth;
    }

    public float getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(float imageHeight) {
        this.imageHeight = imageHeight;
    }

    protected List<HashMap<String, Object>> data;

    public void setData(List<HashMap<String, Object>> data) {
        if (this.data == null) {
            this.data = data;
        } else {
            this.data.clear();
            this.data.addAll(data);
        }
    }

    private Context context;
    protected int blankItemCount;// 空白子项的个数
    protected boolean isRemoved = false;// 是否点击了删除图标

    private boolean isWithText = false;// 图片下面是否带文字

    /**
     * 设置空白区域的个数
     *
     * @param blankItemCount
     */
    public void setBlankItemCount(int blankItemCount) {
        this.blankItemCount = blankItemCount;
    }

    /**
     * 设置是否为删除状态
     *
     * @param isRemoved
     */
    public void setRemoved(boolean isRemoved) {
        this.isRemoved = isRemoved;
    }

    public ImageAdapter(Context context) {
        this(context, false);
    }

    public ImageAdapter(Context context, boolean isWithText) {
        this.isWithText = isWithText;
        this.context = context;

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        final ViewGroup parent) {
        Log.d("ImageAdapter","getView--position="+position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.group_grid_item,
                    null);
            viewHolder.iv_image = (ImageView) convertView
                    .findViewById(R.id.iv_grid_manager_img);
//            if (imageWidth != 0 && imageHeight != 0) {
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.iv_image
//                        .getLayoutParams();
//                Log.e(TAG, "params.width======>" + params.width
//                        + "params.height=====>" + params.height);
//                params.width = (int) imageWidth;
//                params.height = (int) imageHeight;
//                Log.e(TAG, "params.width2======>" + params.width
//                        + "params.height2=====>" + params.height);
//                viewHolder.iv_image.setLayoutParams(params);
//            }

            viewHolder.tv_text = (TextView) convertView
                    .findViewById(R.id.tv_grid_manager_name);
            viewHolder.delete = (ImageView) convertView.findViewById(R.id.iv_grid_delete_icon);
//            viewHolder.badgeView = new BadgeView(context, viewHolder.iv_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_text.setVisibility(View.VISIBLE);

        if (isWithText) {
            if(position==0&&parent.getChildCount()!=0){

            }else{
            String name = (String) data.get(position).get(EasyGridView.TEXT);
            if (name != null && !"".equals(name)) {
                viewHolder.tv_text.setText(name);
            } else {
                viewHolder.tv_text.setVisibility(View.GONE);
            }
            if ("添加".equals(name)) {
                viewHolder.iv_image.setImageDrawable(context.getResources().getDrawable(R.drawable.selector_manager_add));
            } else if ("删除".equals(name)) {
                viewHolder.iv_image.setImageDrawable(context.getResources().getDrawable(R.drawable.selector_manager_delete));
            } else if ("完成".equals(name)) {
                viewHolder.iv_image.setImageDrawable(context.getResources().getDrawable(R.drawable.selector_manager_ok));
            } else {
//                if ((Bitmap) data.get(position).get(
//                        EasyGridView.IMAGE) == null) {
//                    viewHolder.iv_image.setImageDrawable(context.getResources().getDrawable(R.drawable.aboutme_unselected));
//                } else {
//                    viewHolder.iv_image.setImageBitmap((Bitmap) data.get(position).get(
//                            EasyGridView.IMAGE));
//                }
                if(data.get(position).get(
                      EasyGridView.IMAGE)!=null){
                    ImageLoader.getInstance().displayImage(data.get(position).get(
                          EasyGridView.IMAGE).toString(), viewHolder.iv_image, ImageLoaderInterface.optionHeadImage);
                }

            }}
        } else {
            viewHolder.tv_text.setVisibility(View.GONE);
        }
        if (data.size() == 1) {
            if (isRemoved || "完成".equals(data.get(data.size() - 1).get(EasyGridView.TEXT))) {
                if (position != data.size() - 1) {
                    viewHolder.delete.setVisibility(View.VISIBLE);
                }
            } else {
                viewHolder.delete.setVisibility(View.GONE);
            }
        } else if (data.size() > 1) {
            if (!"添加".equals(data.get(data.size() - 2).get(EasyGridView.TEXT)) && "完成".equals(data.get(data.size() - 1).get(EasyGridView.TEXT))) {
                if (position != data.size() - 1) {
                    viewHolder.delete.setVisibility(View.VISIBLE);
                }
            } else {
                viewHolder.delete.setVisibility(View.GONE);
            }
        } else {
            viewHolder.delete.setVisibility(View.GONE);
        }

//        if (isRemoved && position < data.size() - 1) {
//            viewHolder.badgeView.setText(" - ");// 设置删除的头标
//            if (!viewHolder.badgeView.isShown()) {
//                viewHolder.badgeView.setBadgeMargin(0, 0);
//                viewHolder.badgeView
//                        .setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
//                viewHolder.badgeView.show();
//            }
//        }
//        else if (!isRemoved && position < data.size() - blankItemCount) {
//            if (viewHolder.badgeView.isShown()) {
//                viewHolder.badgeView.hide();
//            }
//        }
//        // 空白区域badgeview的控制
//        if (position < data.size() && position >= data.size() - blankItemCount) {
//            if (viewHolder.badgeView.isShown()) {
//                viewHolder.badgeView.hide();
//            }
//        }
        return convertView;
    }

    public static class ViewHolder {
        ImageView iv_image;
        TextView tv_text;
        //        BadgeView badgeView;
        ImageView delete;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}
